package com.example.javapoet;

import com.example.javapoet.entity.User;
import com.example.javapoet.repository.UserRepository;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

/**
 * JavaPoet示例项目的Spring Boot应用程序入口
 */
@SpringBootApplication
public class JavaPoetDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(JavaPoetDemoApplication.class, args);
    }
    
    /**
     * 应用启动后执行的初始化方法
     * 用于测试H2数据源连接
     */
    @Bean
    public CommandLineRunner demo(UserRepository repository) {
        return (args) -> {
            // 保存几个用户记录
            repository.save(new User("张三", "zhangsan@example.com"));
            repository.save(new User("李四", "lisi@example.com"));
            repository.save(new User("王五", "wangwu@example.com"));

            // 获取所有用户
            System.out.println("用户列表:");
            for (User user : repository.findAll()) {
                System.out.println(user.toString());
            }
            System.out.println("H2数据源配置成功!");
        };
    }
}